package org.example;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class TopicPresistentConsumerMain01 {
    public static void main(String[] args) throws Exception {
        presistentTopicConsumer();

    }

    public static void presistentTopicConsumer() throws Exception{
        //1、创建工厂连接对象，需要制定ip和端口号
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.20.158:61617");
        //2、使用连接工厂创建一个连接对象
        Connection connection = connectionFactory.createConnection();
        connection.setClientID("persistent_consumer_client_1");
        //3、开启连接
        connection.start();
        //4、使用连接对象创建会话（session）对象
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5、使用会话对象创建目标对象，包含queue和topic（一对一和一对多）
        Topic topic = session.createTopic("liurq_topic_persistent_01");
        //6、使用会话对象创建生产者对象
        MessageConsumer consumer = session.createDurableSubscriber(topic,"persistent_consumer_client_1");
        //7、向consumer对象中设置一个messageListener对象，用来接收消息
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                // TODO Auto-generated method stub
                if(message instanceof TextMessage){
                    TextMessage textMessage = (TextMessage)message;
                    try {
                        System.out.println("liurq-topic-persistent " + textMessage.getText());
                    } catch (JMSException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

        //8、程序等待接收用户消息
        System.in.read();
        //9、关闭资源
        consumer.close();
        session.close();
        connection.close();
    }
}
